// components/search/cpns/al-focus/al-focus.js
function extractOne(pendingData) {
  // 只有一层的处理逻辑

  // selectedData: [{ superclass: "标签", subclass: ["历史"] }]
  // ->
  // [{type:"select",searchKey: keyName, searchValue：value}]
  // const processedData = []
  let processedData = {}
  pendingData.forEach(item => {
    let temp = {
      type: "select",
      searchKey: item.superclass,
      searchValue: item.subclass.join('-')
    }
    // processedData.push(temp)
    processedData = temp
  })
  return processedData
}

Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    currentPulldown: "书名",
    currentPulldownKey: "book_name",
    isPullDown: false,
    inputValue: ''
  },

  /**
   * 组件的方法列表
   */
  methods: {
    handleSelected(e) {
      // console.log(e.detail);
      const param = {
        ...extractOne(e.detail)
      }
      wx.navigateTo({
        url: '/pages/bookSearchShow/bookSearchShow?searchParam=' + JSON.stringify(param),
      })
    },
    categoryBtn() {
      var popupChildComponent = this.selectComponent('#popup');
      popupChildComponent.show()
    },
    jumpToSearch() {
      const param = {
        type: "input",
        searchKey: this.data.currentPulldownKey,
        searchValue: this.data.inputValue
      }
      wx.navigateTo({
        url: '/pages/bookSearchShow/bookSearchShow?searchParam=' + JSON.stringify(param),
      })
    },

    scanBook() {
      console.log("scan");
      wx.scanCode({
        success(data) {
          const { result } = data
          // 有了结果之后怎么处理
          // 方式一
          // 把扫描的的结果存于全局，reactiveData是个代理对象
          // getApp().globalData.reactiveData.scanResult = result

          // 方式二
          const param = {
            type: "scan",
            searchKey: "book_id",
            searchValue: result
          }
          wx.navigateTo({
            url: '/pages/bookSearchShow/bookSearchShow?searchParam=' + JSON.stringify(param),
          })
        },
        fail(err) {
          wx.showToast({
            title: '识别失败，请重试',
            icon: "error"
          })
        }
      })
    },
    clearInputValue() {
      console.log('dzl');
      this.setData({
        inputValue: ''
      })
      // console.log(this.data.inputValue);
    },
    getInputValue(e) {
      this.setData({
        inputValue: e.detail.value
      })
    },
    // handleCancel() {
    //   this.triggerEvent("cancelSearch", { isFocus: false })
    // },
    isPullDown() {
      this.setData({
        isPullDown: !this.data.isPullDown
      })
    },
    handlePullDown(e) {
      const { index, item } = e.detail
      this.setData({
        currentPulldown: item.name,
        currentPulldownKey: item.key
      })
    }
  }
})
